package com.sound.ampache;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/

import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Random;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import com.sound.ampache.objects.Album;
import com.sound.ampache.objects.Artist;
import com.sound.ampache.objects.Playlist;
import com.sound.ampache.objects.Song;
import com.sound.ampache.objects.Tag;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;



class SongPlaylist extends MainScreen implements HTTPTransportListener {
	
	public Vector songPlaylist = new Vector();
	private Dialog statusDlg;
	
	SongPlaylist()
	{
		Bitmap icon = Bitmap.getBitmapResource("img/download.png"); 
		statusDlg = new Dialog("Downloading...",null,null,0,icon); 
	}
	
	
	public void addAlbumToPlaylist(Album album) {
		statusDlg.show();
		try {
	        ampacheberry.comm.setHTTPTransportListener(this);
	        ampacheberry.comm.send("album_songs", album.id); 
	
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	
	public void addPlaylistToPlaylist(Playlist playlist) {
		statusDlg.show();
		try {
	        ampacheberry.comm.setHTTPTransportListener(this);
	        ampacheberry.comm.send("playlist_songs", playlist.id); 
	
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	
	public void addArtistToPlaylist(Artist artist) {
		statusDlg.show();
		try {
	        ampacheberry.comm.setHTTPTransportListener(this);
	        ampacheberry.comm.send("artist_songs", artist.id); 
	
		} catch (Exception e1) {
			e1.printStackTrace();
		}		
	}
	
	
	public void addTagToPlaylist(Tag tag) {
		statusDlg.show();
		try {
	        ampacheberry.comm.setHTTPTransportListener(this);
	        ampacheberry.comm.send("tag_songs", tag.id); 
	
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	
	public void addSongToPlaylist(Song song) {
		Vector thisSong = new Vector();
    	thisSong.addElement(song);
    	processResponse(thisSong);
    }


	
	public void writePlaylist() {
    	String playlistContents = "#EXTM3U\n";
    	
    	for (Enumeration e = songPlaylist.elements(); e.hasMoreElements();)
    	{
    		Song thisSong = new Song();
    		thisSong = (Song) e.nextElement();
    		playlistContents += "#EXTINF:" + 
    			thisSong.time + "," + 
    			thisSong.artist + " - " +
    			thisSong.name + "\n" +
    			thisSong.url + "\n";
    	}
    	
    	//String fullPath = "file:///SDCard/ampache_playlist.txt";
    	String fullPath = "file:///store/home/user/music/ampache_playlist.m3u";

    	try {
    	    FileConnection fconn = (FileConnection) Connector.open(fullPath, Connector.READ_WRITE);

    	    if (fconn.exists()) {
    	        fconn.delete();
    	    }

    	    fconn.create();
    	    OutputStream os = fconn.openOutputStream();
    	    os.write(playlistContents.getBytes());
    	    os.close();
    	    fconn.close();
    	}
    	catch (IOException e) {
    	    System.out.println("Oh no!!!! " + e.toString());
    	}
	}

	
	public void processResponse(final Vector data) {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run()
            {
    	    	for (Enumeration e = data.elements(); e.hasMoreElements();)
    	    	{
    	    		Song thisSong = new Song();
    	    		thisSong = (Song) e.nextElement();
    	    		songPlaylist.addElement(thisSong);
    	    	}
    	    	
    	    	writePlaylist();
    	    	ampacheberry.playlistScreen.getList();
    	    	statusDlg.close();
            }
        });	
	}
	
	
	public void clearPlaylist() {
			songPlaylist.removeAllElements();
	}
	
	
	public void delSong(int songNum) {
		songPlaylist.removeElementAt(songNum);
	}
	
} 
